package com.gkzf.ai.module.crm.config;

import io.netty.channel.ChannelOption;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;

@Configuration
public class WebClientConfig {
    @Bean
    public WebClient webClient() {
        HttpClient httpClient = HttpClient.create()
                // 设置连接建立超时，例如 5 秒 (建议保留，不无限等待连接建立)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000);
        // **** 移除 .responseTimeout(Duration.ofSeconds(600)) ****
        // 移除 responseTimeout 会使得 WebClient 依赖于底层 TCP/OS 的默认超时行为
        // 对于长时间不活动的长连接，这可能意味着等待很久（甚至几小时）才认为连接断开

        // 配置编解码器，确保支持 SSE 和 JSON
        ExchangeStrategies strategies = ExchangeStrategies.builder().build();


        return WebClient.builder()
                // .baseUrl("some-base-url-if-applicable") // 或者直接在 uri() 中使用完整 URL
                .clientConnector(new ReactorClientHttpConnector(httpClient))
                .exchangeStrategies(strategies) // 应用编解码器配置
                .build();
    }
}
